<template>
    <div class = "login-box">
        <el-form
    ref="ruleFormRef"
    :model="ruleForm"
    status-icon
    :rules="rules"
    label-width="0"
    class="demo-ruleForm"
  >
  <h1>叩丁狼后台管理系统</h1>
    <el-form-item  prop="username">
      <el-input v-model="ruleForm.username" type="username" placeholder="请输入用户名" autocomplete="off"  :prefix-icon="UserFilled"  size="large"/>
    </el-form-item>
    <el-form-item  prop="password">
      <el-input
        v-model="ruleForm.password"
        placeholder="请输入密码"
        type="password"
        autocomplete="off"
        :prefix-icon="Lock"
        size="large"
      />
    </el-form-item>
   
    <el-form-item>
      <el-button type="primary" size="large" @click="submitForm(ruleFormRef)"
        >登录</el-button
      >
      
    </el-form-item>
  </el-form>
     
    </div>
</template>

<script setup lang="ts">


import { reactive, ref } from 'vue'
import { ElMessage, FormInstance } from 'element-plus'
import { Lock, UserFilled } from '@element-plus/icons-vue'
import {user} from "../../api"
import { useRouter } from 'vue-router'
import { useUserStore } from '../../store'


let userStore = useUserStore()
let router =  useRouter()
const ruleFormRef = ref<FormInstance>()


const validatePass = (rule: any, value: string, callback: any) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  } else {
    callback()
  }
}

const validatePass2 = (rule:any, value:string, callback:any) => {
  if (value === '') {
    callback(new Error('请输入用户名'))
  } else {
    callback()
  }
    }


const ruleForm = reactive({
  username: '',
  password: '',

})

const rules = reactive({
  username: [{validator: validatePass2,required:true, trigger: 'blur' }],
  password: [{ validator: validatePass, trigger: 'blur' }],

})

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid) => {
    // if (valid) {
    //   console.log('submit!')
    // } else {
    //   console.log('error submit!')
    //   return false
    // }
    if(!valid){
      return false
    }
    user.loginApi(ruleForm).then((res)=>{
      if(res.code ===200){
        //获取token
        localStorage.setItem("token",res.data.tokenHead+res.data.token);
        ElMessage.success("登录成功")
        router.push('/home')

        //登录成功后，需要请求用户的菜单数据
        user.userInfoApi().then((res)=>{
          if(res.code === 200){
            //得到该用户所有菜单数据
            //把数据存到pinia
            console.log(res.data);
            userStore.menus = res.data.menus

            
          }
        })
      }else{
        ElMessage.error('账号或密码错误')
      }
     
      
    })
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}
</script>

<style lang="less" scoped>
.login-box{
    width: 400px;
    margin: 200px auto;
}
::v-deep.el-button{
    width: 100%;
}
::v-deep.el-input__wrapper{
  background-color: #000;
}
h1{
    color: #fff;
    text-align: center;
}
</style>